﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Drawing.Printing;
using VM.Common.Result;
using VM.Entity.EntityMap.DataBase;
using VM.Entity.EntityMap.Menu;
using VM.Entity.EntityMap.searchLog;
using VM.Entity.EntityMap.Topic;
using VM.IBLL.SearchLog;
using VM.IBLL.Topic;
using VM.WebCore.Jwt;
using VM.WebCore.SwaggerExtend;
using VM.Wechat;

namespace VM.WebAPI.Controllers
{


    /// <summary>
    /// 数据字典相关控制器
    /// </summary>
    [Route("api/searchLog")]
    [ApiController]
    public class SearchLogController : ControllerBase
    {
        private readonly ISearchLogBLL _ISearchLogBLL;
        private readonly IJWTService _IJWTService;
        private readonly IHttpContextAccessor _HttpContextAccessor;


        public SearchLogController(IJWTService iJWTService, ISearchLogBLL iSearchLogBLL, IHttpContextAccessor httpContextAccessor)
        {

            _ISearchLogBLL = iSearchLogBLL;
            _IJWTService = iJWTService;
            _HttpContextAccessor = httpContextAccessor;
        }



        /// <summary>
        /// 添加搜索记录 PC+PHONE
        /// </summary>
        /// <param name="sys_searchLog">输入的搜索信息</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("addSearchLog")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public async Task< ApiDataResult<sys_searchLog>> addDataBaseType(sys_searchLog obj)
        {
            int UserId = 0;

            //获取到header 内的用户信息进行解析
            var authorizationHeader = _HttpContextAccessor.HttpContext.Request.Headers["Authorization"];
            //解析jwt
            string tokenInfo = authorizationHeader.ToString().Replace("bearer ", "").Replace("Bearer ", "");
            var AESId = _IJWTService.AnaliseToken(tokenInfo, "id");
            //解析userid
            UserId = Convert.ToInt32(AESUntil.Decrypt(AESId));

            obj.createUserId = UserId;
            obj.createDate = DateTime.Now;
            obj.UseQty = 1;

            //判断当前用户当前id是否已存在，如果已存在，则只更新
            //校验验证码是否可用，如果可用，返回true
            ApiDataResult<PagingData<sys_searchLog>> ResGet = _ISearchLogBLL.getSearchLogList(UserId,obj.name, obj.menuId, 1, 1);
            if (ResGet.code==200&&ResGet.msg!="无数据") 
            {
                int id = Convert.ToInt32(ResGet.data.dataList[0].id);
                sys_searchLog searcheLog = new sys_searchLog();
                searcheLog.id = id;
                searcheLog.UseQty = ResGet.data.dataList[0].UseQty + 1;

                bool UpdateRes = await _ISearchLogBLL.updateSearchLog(searcheLog);
                ApiDataResult<sys_searchLog> res = new ApiDataResult<sys_searchLog>();
                if (UpdateRes)
                { 
                    res.code = 200;
                    res.data = null;
                    res.msg = "更新成功";
                }
                else
                {
                    res.code = 403;
                    res.data = null;
                    res.msg = "更新失败";

                }
               
                return res;
            }
            else
            {
                return _ISearchLogBLL.addSearchLog(obj);
            }
        }



        /// <summary>
        /// 获取搜索记录列表，   PC+PHONE
        /// </summary>
        /// <param name="menuId">菜单内码</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getSearchLogList")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<PagingData<sys_searchLog>> getDataBaseTypeList(string? name, int  menuId , int pageIndex = 1, int pageSize = 100)
        {
            int UserId = 0;

            //获取到header 内的用户信息进行解析
            var authorizationHeader = _HttpContextAccessor.HttpContext.Request.Headers["Authorization"];
            //解析jwt
            string tokenInfo = authorizationHeader.ToString().Replace("bearer ", "").Replace("Bearer ", "");
            var AESId = _IJWTService.AnaliseToken(tokenInfo, "id");
            //解析userid
            UserId =Convert.ToInt32(  AESUntil.Decrypt(AESId));

            //校验验证码是否可用，如果可用，返回true
            ApiDataResult<PagingData<sys_searchLog>> Res = _ISearchLogBLL.getSearchLogList(  UserId, name, menuId, pageIndex,pageSize);
            return Res;
        }



       










    }
}
